package com.bootdo.slideshow.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.mall.domain.MallCateGory;
import com.bootdo.mall.domain.MallGood;
import com.bootdo.mall.service.MallCateGoryService;
import com.bootdo.mall.service.MallGoodService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.slideshow.domain.CommonsSlideshow;
import com.bootdo.slideshow.service.CommonsSlideshowService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author lujingqian
 * @email 2100272728@qq.com
 * @date 2019-11-12 10:13:13
 */
 
@Controller
@RequestMapping("/slideshow/commonsSlideshow")
public class CommonsSlideshowController extends BaseController{


	@Autowired
	private CommonsSlideshowService commonsSlideshowService;
	@Autowired
	private MallGoodService mallGoodService;
	@Autowired
	private MallCateGoryService mallCateGoryService;

	@ModelAttribute
	public CommonsSlideshow get(@RequestParam(required=false) Integer id) {
		if (id != null){
			return commonsSlideshowService.get(id);
		}else{
			return new CommonsSlideshow();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/slideshow/commonsSlideshow','slideshow:commonsSlideshow:commonsSlideshow',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','slideshow:commonsSlideshow:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("slideshow:commonsSlideshow:commonsSlideshow")	
	String CommonsSlideshow(CommonsSlideshow commonsSlideshow,HttpServletRequest request,HttpServletResponse response){
	    return "slideshow/commonsSlideshow/commonsSlideshow";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("slideshow:commonsSlideshow:commonsSlideshow")
	public PageUtils list(@RequestParam Map<String, Object> params){
	//	params.put("isDel","0");
		//查询列表数据
        Query query = new Query(params);
		List<CommonsSlideshow> commonsSlideshowList = commonsSlideshowService.list(query);
		int total = commonsSlideshowService.count(query);
		PageUtils pageUtils = new PageUtils(commonsSlideshowList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("slideshow:commonsSlideshow:edit")
	String add(CommonsSlideshow commonsSlideshow,HttpServletRequest request,HttpServletResponse response){
	    return "slideshow/commonsSlideshow/commonsSlideshowedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(CommonsSlideshow commonsSlideshow) {
		return R.ok().put("i",commonsSlideshow);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("slideshow:commonsSlideshow:edit")
	public R save( CommonsSlideshow commonsSlideshow){
		if(!commonsSlideshow.getResourceUrl().equals("0")){
			String resourceUrl = commonsSlideshow.getResourceUrl();
			try {
				MallGood mallGood = mallGoodService.get(Long.parseLong(resourceUrl));
				MallCateGory mallCateGory = mallCateGoryService.get(Integer.parseInt(resourceUrl));

				if(mallGood==null&&mallCateGory==null){
					return R.error("关联商品不存在");
				}

			}catch (Exception e){
			    e.printStackTrace();
				return R.error("关联商品不存在");
			}
		}
		if(commonsSlideshowService.save(commonsSlideshow)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("slideshow:commonsSlideshow:edit")
	public R remove( Integer id){
		if(commonsSlideshowService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("slideshow:commonsSlideshow:edit")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		commonsSlideshowService.batchRemove(ids);
		return R.ok();
	}
	
}